var http = require('http')
//1.创建server
var server = http.createServer()

//2.监听request事件，设置请求处理函数
server.on('request', function(req,response){
    console.log('收到请求了，请求路径是'+req.url)
    console.log('请求我的客户端的ip是'+req.socket.remoteAddress+'端口号:'+req.socket.remotePort)

    // response.write('hello nodejs')
    // response.end()
    //根据不同路径发送不同响应结果
    //1.获取请求路径
    //2.判断路径响应
    var url = req.url
    if(url === '/'){
        response.end('index page')
    }else if(url === '/login'){
        response.end('login page')
    }else if(url === '/products'){
        var products = [
            {
                name: "iphone",
                price: 8888
            },
            {
                name: "iphonex",
                price: 6666
            },
            {
                name: "iphone12",
                price: 9999
            }
        ]
        //响应内容只能是二进制数字或者字符串
        response.end(JSON.stringify(products))
    }else{
        response.end('404 Not Found')
    }

    
})

//3.绑定端口号 启动服务
server.listen(8000,function(){
    console.log('服务器启动成功，可以访问了')
})